<?php

class TestController extends Controller
{
	public $layout='column1';

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
		// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
		),
		// page action renders "static" pages stored under 'protected/views/site/pages'
		// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
		),
		);
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
			echo $error['message'];
			else
			$this->render('error', $error);
		}
	}
	//AferDelete, AferUpdate
	public function actionTranfer()
	{
		if(Yii::app()->user->id == 2){
			// Connect to MSSQL
			$link = mssql_connect('192.168.5.137', 'sohagiupban', 'giupban123!@#');
			if (!$link) {
				die('Something went wrong while connecting to MSSQL');
			} else {
				$arrPj1 = array(436,437,86,427,467,428,407,452,448,444,443,442,441,435,95);
				$arrPj2 = array(469,468,466,464,463,462,460,459,458,457,455,454,453,451,447,446,
				445,440,439,438,431,430,429,414,406,405,404,399,396,387,375,368,352,351,350,348,
				345,343,293,292,289,288,287,285,279,277,276,275,274,270,261,254,251,249,248,247,
				246,245,243,242,241,240,239,237,206,205,180,140,132,125,120,119,118,107,100,99,
				96,88,87,83,82,78,71,67,48,45,28,4);
				$strPj1 = implode(',',$arrPj1);
				$strPj2 = implode(',',$arrPj2);
				mb_internal_encoding('UTF-8');
				$criteria = new CDbCriteria;
				$criteria->addInCondition('projectID',$arrPj2);
				$criteria->order = 'oid DESC';
				$news = News::model()->find($criteria);
				mssql_select_db("LinkhayData", $link);
				if($news){
					$offset = $news->oid;
					echo $news->title;
				} else {
					$offset = 0;
				}
				$sql= "SELECT TOP 5000 * FROM tblTintuc WHERE projectID in (".$strPj2.") AND id > ".$offset." ORDER BY [id] ASC";
				$rs= mssql_query($sql, $link);
				header('Content-Type: text/html; charset=utf8');
				do {
					while ($row = mssql_fetch_array($rs)){
						$post = new News;
						$post->catid = $row['catid'];
						$post->title = $row['title'];
						$post->subtitle = $row['subtitle'];
						$post->summary = $row['summary'];
						$post->content = $row['content'];
						$post->urlSource = $row['urlSource'];
						$post->HTMLSource = $row['HTMLSource'];
						$post->NewType = $row['NewType'];
						$post->imageSRC = $row['imageSRC'];
						$post->ConfigID = $row['ConfigID'];
						$post->IP = $row['IP'];
						$post->isOk = $row['isOk'];
						$post->isLoad = $row['isLoad'];
						$post->isRss = $row['isRss'];
						$post->VideoURL = $row['VideoURL'];
						$post->datePost = $row['datePost'];
						$post->IndexTime = $row['IndexTime'];
						$post->projectID = $row['projectID'];
						$post->isHotNew = $row['isHotNew'];
						$post->istool = $row['istool'];
						$post->oid = $row['id'];
						$post->save();
						//echo $row['title'].'đỗ thái';
					}
				} while ( mssql_next_result($rs) );
				mssql_close($link);
				header('Location: http://www.giupban.vn/quanly/test/tranfer');
				//die('Connection very cool');
			}
		}
	}
	public function actionSelect(){
		set_time_limit(100);
		$newsType = array(0=>'Toàn bộ', 436=>'xinhxinh.com.vn',437=>'eva.vn',86=>'danong.com',427=>'afamily.vn',467=>'genk.vn',428=>'dantri.com.vn',407=>'vnexpress.net',452=>'monngonhanoi.com',448=>'suckhoecongdong.com',444=>'bibi.vn',443=>'tapchilamdep.com',442=>'phununet.com',441=>'dep.com.vn',435=>'camnanggiadinh.com.vn',95=>'giadinh.net.vn',
			469=>'docbao.vn',468=>'vietgiaitri.com',466=>'genk.vn',464=>'batdongsan.com.vn',463=>'2sao.vn',462=>'vef.vn',460=>'thuocbietduoc.com.vn',459=>'thuocbietduoc.com.vn/hoi-dap',458=>'movie.zing.vn',457=>'game.zing.vn',455=>'thethaovanhoa.vn',454=>'vnmedia.vn',453=>'thegioidienanh.vn',451=>'vietstock.vn',447=>'kenh14.vn',446=>'chonnoithat.com.vn',
			445=>'nhadep-magazine.com',440=>'tienphong.vn',439=>'ngoisao.net',438=>'suckhoedoisong.vn',431=>'dothi.net',430=>'stox.vn',429=>'itcenter.vn',414=>'aohaiday.com',406=>'svvn.vn',405=>'phununet.com',404=>'tinnhanhblog.com',399=>'thegioixaydung.wordpress.com',396=>'kythuatmaytinh.wordpress.com',387=>'tapchilinux.com',375=>'giavang.net',368=>'antoanmang.vn',352=>'vietseo.net',351=>'pc4viet.com',350=>'tin247.com',348=>'tinmoi.vn',
			345=>'docbao.vn',343=>'laodong.com.vn',293=>'kienthuckinhte.com',292=>'thuongvuvietnam.gov.vn',289=>'viettimes.com.vn',288=>'baokinhteht.com.vn',287=>'vietstock.com.vn',285=>'baodatviet.vn',279=>'taichinhdientu.vn',277=>'asset.vn',276=>'tiasang.com.vn',275=>'ktdt.com.vn',274=>'dddn.com.vn',270=>'tintuconline.com.vn',261=>'tinhte.com',254=>'khampha24h.com',251=>'doithoaitre.vtv.vn',249=>'gioitinhtuoiteen.org.vn',248=>'tincongnghe.com',247=>'web2vietnam.com',
			246=>'cntt.vn',245=>'thongtincongnghe.com',243=>'quantrimang.com',242=>'congnghemoi.net',241=>'tuanvietnam.net',240=>'ictnews.vn',239=>'thehe8x.net',237=>'saga.vn',206=>'tienphong.vn',205=>'nhandan.com.vn',180=>'thethaovanhoa.vn',140=>'baocongthuong.com.vn',132=>'xahoithongtin.com.vn',125=>'netlife.com.vn',120=>'thegioitrithuc.vn',119=>'cafef.vn',118=>'thanhnien.com.vn',107=>'diaoconline.vn',100=>'hoahoctro.vn',99=>'vtc.vn',
			96=>'phapluattp.vn',88=>'vnmedia.vn',87=>'hanoimoi.com.vn',83=>'suctrevietnam.com',82=>'ngoisao',78=>'kenh14',71=>'new so hoa',67=>'cuocsongso.vtv.vn',48=>'pcworld.com.vn',45=>'khoahoc.com.vn',28=>'vneconomy',4=>'vnexpress');
		$criteria = new CDbCriteria;
		$criteria->select = 'id, title, summary, projectID, imageSRC, selected_f, urlSource, oid';
		//$criteria->order = 'oid DESC';

		if(isset($_GET['type'])&&$_GET['type']){
			$criteria->compare('projectID',$_GET['type']);
		}
		if(isset($_GET['key-news'])&&$_GET['key-news']){
			$criteria->addSearchCondition('title',$_GET['key-news']);
		}

		$pagination = new CPagination(News::model()->count($criteria));
		$pagination->pageSize = 100;
		$pagination->applyLimit($criteria);
		if(isset($_GET['type'])&&$_GET['type']){
			$pagination->params['type'] = $_GET['type'];
		}
		if(isset($_GET['key-news'])&&$_GET['key-news']){
			$pagination->params['key-news'] = $_GET['key-news'];
		}
		$criteria->limit = 10;
		$news = News::model()->findAll($criteria);
		$connection=Yii::app()->db;
		$sql='select wp_terms.term_id, wp_terms.name, wp_term_taxonomy.term_taxonomy_id from wp_terms
			left join wp_term_taxonomy on wp_terms.term_id =
			wp_term_taxonomy.term_id
			where taxonomy= "category"';
		$command=$connection->createCommand($sql);
		$rows = $command->query();
		$categories = $rows->readAll();
		$category = array();
		foreach($categories as $ca){
			$category[$ca['term_taxonomy_id']] = $ca['name'];
		}

		$this->render('select',array('news'=>$news,'category'=>$category,'pages'=>$pagination,'types'=>$newsType));
	}
	public function actionSave(){
		$this->layout = 'empty';
		$id = $_POST['id'];
		$cid = $_POST['cid'];

		if(!$id && !$cid){
			echo 'Bài viết không tồn tại!';die;
		} else {
			$post = News::model()->findByPk($id);
			if($post->selected_f){
				echo 'Bài đã chọn!';die;
			}

			$connection=Yii::app()->db;
			$sql='INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, comment_status, ping_status, post_name, post_modified, post_parent, post_type) VALUES(:post_author, :post_date, :post_date_gmt, :post_content, :post_title, :post_status, :comment_status, :ping_status, :post_name, :post_modified, :post_parent, :post_type)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":post_author",2,PDO::PARAM_INT);
			$command->bindValue(":post_date",date('Y-m-d H:i:s', time()),PDO::PARAM_STR);
			$command->bindValue(":post_date_gmt",date('Y-m-d H:i:s', time()),PDO::PARAM_STR);
			$command->bindValue(":post_content",$post->content.'<br />Nguồn: <span style="color:#CCC;">'.$post->urlSource.'</span>',PDO::PARAM_STR);
			$command->bindValue(":post_title",$post->title,PDO::PARAM_STR);
			$command->bindValue(":post_status",'publish',PDO::PARAM_STR);
			$command->bindValue(":comment_status",'open',PDO::PARAM_STR);
			$command->bindValue(":ping_status",'open',PDO::PARAM_STR);
			$command->bindValue(":post_name",$this->removeMarkInVn($post->title),PDO::PARAM_STR);
			$command->bindValue(":post_modified",date('Y-m-d H:i:s', time()),PDO::PARAM_STR);
			$command->bindValue(":post_parent",0,PDO::PARAM_INT);
			$command->bindValue(":post_type",'post',PDO::PARAM_STR);
			$rows = $command->execute();
			$pId = $connection->getLastInsertId();

			$sql='INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES(:object_id, :term_taxonomy_id)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":object_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":term_taxonomy_id",$_POST['cid'],PDO::PARAM_INT);
			$rows = $command->execute();
			
			$sql='INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES(:post_id, :meta_key, :meta_value)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":post_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":meta_key",'_wp_attached_file',PDO::PARAM_STR);
			$command->bindValue(":meta_value",$post->imageSRC,PDO::PARAM_STR);
			$rows = $command->execute();
			$sql='INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES(:post_id, :meta_key, :meta_value)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":post_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":meta_key",'_wp_attachment_metadata',PDO::PARAM_STR);
			$command->bindValue(":meta_value",'',PDO::PARAM_STR);
			$rows = $command->execute();
			$sql='INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES(:post_id, :meta_key, :meta_value)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":post_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":meta_key",'_mf_write_panel_id',PDO::PARAM_STR);
			$command->bindValue(":meta_value",1,PDO::PARAM_INT);
			$rows = $command->execute();
			
			$sql='INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES(:post_id, :meta_key, :meta_value)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":post_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":meta_key",'description',PDO::PARAM_STR);
			$command->bindValue(":meta_value",$post->summary,PDO::PARAM_STR);
			$rows = $command->execute();
			$mId = $connection->getLastInsertId();
			
			$sql='INSERT INTO wp_mf_post_meta (id, group_count, field_count, post_id, field_name, order_id) VALUES(:id, :group_count, :field_count, :post_id,:field_name, :order_id)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":id",$mId,PDO::PARAM_INT);
			$command->bindValue(":group_count",1,PDO::PARAM_INT);
			$command->bindValue(":field_count",1,PDO::PARAM_INT);
			$command->bindValue(":post_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":field_name",'description',PDO::PARAM_STR);
			$command->bindValue(":order_id",1,PDO::PARAM_INT);
			$rows = $command->execute();
			
			$sql='INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, comment_status, ping_status, post_name, post_modified, post_parent, post_type, post_mime_type, guid) VALUES(:post_author, :post_date, :post_date_gmt, :post_content, :post_title, :post_status, :comment_status, :ping_status, :post_name, :post_modified, :post_parent, :post_type, :post_mime_type, :guid)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":post_author",1,PDO::PARAM_INT);
			$command->bindValue(":post_date",date('Y-m-d H:i:s', time()),PDO::PARAM_STR);
			$command->bindValue(":post_date_gmt",date('Y-m-d H:i:s', time()),PDO::PARAM_STR);
			$command->bindValue(":post_content",'',PDO::PARAM_STR);
			$command->bindValue(":post_title",'IMAGE_'.$pId,PDO::PARAM_STR);
			$command->bindValue(":post_status",'inherit',PDO::PARAM_STR);
			$command->bindValue(":comment_status",'open',PDO::PARAM_STR);
			$command->bindValue(":ping_status",'open',PDO::PARAM_STR);
			$command->bindValue(":post_name",'image_'.$pId,PDO::PARAM_STR);
			$command->bindValue(":post_modified",date('Y-m-d H:i:s', time()),PDO::PARAM_STR);
			$command->bindValue(":post_parent",$pId,PDO::PARAM_INT);
			$command->bindValue(":post_type",'attachment',PDO::PARAM_STR);
			$command->bindValue(":post_mime_type",'image/jpeg',PDO::PARAM_STR);
			$command->bindValue(":guid",$post->imageSRC,PDO::PARAM_STR);
			$rows = $command->execute();
			$pmId = $connection->getLastInsertId();
			
			$sql='INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES(:post_id, :meta_key, :meta_value)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":post_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":meta_key",'image',PDO::PARAM_STR);
			$command->bindValue(":meta_value",$pmId,PDO::PARAM_INT);
			$rows = $command->execute();
			$mId = $connection->getLastInsertId();

			$sql='INSERT INTO wp_mf_post_meta (id, group_count, field_count, post_id, field_name, order_id) VALUES(:id, :group_count, :field_count, :post_id,:field_name, :order_id)';
			$command=$connection->createCommand($sql);
			$command->bindValue(":id",$mId,PDO::PARAM_INT);
			$command->bindValue(":group_count",1,PDO::PARAM_INT);
			$command->bindValue(":field_count",1,PDO::PARAM_INT);
			$command->bindValue(":post_id",$pId,PDO::PARAM_INT);
			$command->bindValue(":field_name",'image',PDO::PARAM_STR);
			$command->bindValue(":order_id",1,PDO::PARAM_INT);
			$rows = $command->execute();
			$post->selected_f = 1;
			$post->save();
			echo "1";die;
		}
	}
	public function removeMarkInVn($stringVN) {
		$markVN=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
            "ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
            ,"ế","ệ","ể","ễ",
            "ì","í","ị","ỉ","ĩ",
            "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
            ,"ờ","ớ","ợ","ở","ỡ",
            "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
            "ỳ","ý","ỵ","ỷ","ỹ",
            "đ",
            "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
            ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
            "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
            "Ì","Í","Ị","Ỉ","Ĩ",
            "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
            ,"Ờ","Ớ","Ợ","Ở","Ỡ",
            "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
            "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
            "Đ"," ");

            $unMark=array("a","a","a","a","a","a","a","a","a","a","a"
            ,"a","a","a","a","a","a",
            "e","e","e","e","e","e","e","e","e","e","e",
            "i","i","i","i","i",
            "o","o","o","o","o","o","o","o","o","o","o","o"
            ,"o","o","o","o","o",
            "u","u","u","u","u","u","u","u","u","u","u",
            "y","y","y","y","y",
            "d",
            "A","A","A","A","A","A","A","A","A","A","A","A"
            ,"A","A","A","A","A",
            "E","E","E","E","E","E","E","E","E","E","E",
            "I","I","I","I","I",
            "O","O","O","O","O","O","O","O","O","O","O","O"
            ,"O","O","O","O","O",
            "U","U","U","U","U","U","U","U","U","U","U",
            "Y","Y","Y","Y","Y",
            "D","-");

            return trim(preg_replace('/[^a-zA-Z0-9\-_]/','',strtolower(str_replace($markVN, $unMark, $stringVN))),"-");
	}
}